package cn.itrip.service;

import cn.itrip.bean.ItripHotelVO;
import cn.itrip.common.*;
import cn.itrip.vo.hotel.SearchHotCityVO;
import cn.itrip.vo.hotel.SearchHotelVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Sort;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.core.query.Criteria;
import org.springframework.data.solr.core.query.Query;
import org.springframework.data.solr.core.query.SimpleQuery;
import org.springframework.data.solr.core.query.result.ScoredPage;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SearchServiceImpl implements SearchService {

    @Resource
    private SolrTemplate solrTemplate;

    /**
     * 
     * 酒店分页查询
     * @param vo 
     * @return
     * @throws Exception
     */
    @Override
    public Page searchItripHotelPage(SearchHotelVO vo) throws Exception {
        //创建Query查询对象
        Query query = new SimpleQuery("*:*");

        //使用Criteria拼接条件 目的地
        Criteria criteria=new Criteria("destination");
        if (EmptyUtils.isNotEmpty(vo.getDestination())){
            criteria = criteria.contains(vo.getDestination());
        }
        //关键字
        if (EmptyUtils.isNotEmpty(vo.getKeywords())){
            criteria=criteria.and("keyword").contains(vo.getKeywords());
        }
        //商圈
        if (EmptyUtils.isNotEmpty(vo.getTradeAreaIds())){
            String tradeAreaIds = vo.getTradeAreaIds();
            String[] split = tradeAreaIds.split(",");
            for (String s:split) {
                criteria.and("tradingAreaIds").contains(","+s+",");
            }
        }
        //最低价小于客户传入的值对大值
        if (EmptyUtils.isEmpty(vo.getMaxPrice())){
            criteria=criteria.and("minPrice").lessThanEqual(vo.getMaxPrice());
        }
        //最低价格大于客户传入的最低值
        if (EmptyUtils.isEmpty(vo.getMinPrice())){
            criteria=criteria.and("minPrice").greaterThanEqual(vo.getMinPrice());
        }
        //星级
        if (EmptyUtils.isNotEmpty(vo.getHotelLevel())){
            criteria=criteria.and("hotelLevel").is(vo.getHotelLevel());
        }
        //特色
        if (EmptyUtils.isNotEmpty(vo.getFeatureIds())){
            String[] featureIds = vo.getFeatureIds().split(",");
            for (String featureId : featureIds) {
                criteria=criteria.and("featureIds").contains(","+featureId+",");

            }
        }
        //升序排序
        if (EmptyUtils.isNotEmpty(vo.getAscSort())){
            query.addSort(new Sort(Sort.Direction.ASC,vo.getAscSort()));
        }
        //降序排序
        if (EmptyUtils.isNotEmpty(vo.getAscSort())){
            query.addSort(new Sort(Sort.Direction.DESC,vo.getAscSort()));
        }

        //分页 当前也
        Integer pageNo = Constants.DEFAULT_PAGE_NO;
        if (EmptyUtils.isNotEmpty(vo.getPageNo())){
            pageNo=vo.getPageNo();
        }
        //显示条数
        Integer pageSize = Constants.DEFAULT_PAGE_SIZE;
        if (EmptyUtils.isNotEmpty(vo.getPageSize())){
            pageSize=vo.getPageSize();
        }

        //设置solr分页
        query.setOffset((pageNo-1)*pageSize);
        query.setRows(pageSize);
        //把拼接的条件放入query中
        query.addCriteria(criteria);

        //查询solr    存放到page中
        ScoredPage<ItripHotelVO> itripHotelVOS = solrTemplate.queryForPage(query, ItripHotelVO.class);

        Page page=new Page(pageNo,pageSize,new Long(itripHotelVOS.getTotalElements()).intValue());
        page.setRows(itripHotelVOS.getContent());

        return page;
    }


    /**
     *  根据热门城市查询酒店
     * @param cityVo
     * @return
     * @throws Exception
     */
    @Override
    public List<ItripHotelVO> searchSearchHotCityVO(SearchHotCityVO cityVo) throws Exception {
        Query query=new SimpleQuery("*:*");
        Criteria criteria=new Criteria("cityId");
        criteria.is(cityVo.getCityId());
        query.addCriteria(criteria);
        ScoredPage<ItripHotelVO> itripHotelVOS = solrTemplate.queryForPage(query, ItripHotelVO.class);
        return itripHotelVOS.getContent();
    }

}
